// pages/membership_score/membership_score.js
const request = require('../../utils/promise.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userId: null,
    scoreArr: [],
    page: 0,
    pageSize: 16
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.userId = options.userId;
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.reqUserScoreDetail();
  },

  /**
   * 页面被用户分享时执行
   */
  onShareAppMessage() {
    return {
      title: '公社积分'
    }
  },

  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: '公社积分'
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.reqUserScoreDetail();
    wx.stopPullDownRefresh();
  },

  onReachBottom() {
    this.reqUserScoreDetail();
  },

  /**
   * 获取公社积分 得分记录
   */
  reqUserScoreDetail() {
    const param = {userId: this.data.userId, page: this.data.page, pageSize: this.data.pageSize}
    request.GET('/user/scoreRecords', param).then(res => {
      // console.log(res)
      if (res.data.resCode==200) {
        let currPage = this.data.page
        if (res.data.scoreArr.length!==0) {
          currPage++
        }
        let tempArr = this.data.scoreArr
        tempArr = tempArr.concat(res.data.scoreArr)

        this.setData({
          page: currPage,
          scoreArr: tempArr
        })
      } else {
        wx.showToast({
          title: '获取数据异常',
          icon: 'error'
        })
      }
    })
  },

  /**
   * 点击查看项目项目详情
   */
  clickViewScoreDetail(e) {
    // wx.navigateTo({
    //   url: '/pages/score_detail/score_detail?projectId=47&userId='+ this.data.userId,
    // })
    wx.navigateTo({
      url: '/pages/score_detail_desc/score_detail_desc?id='+ e.currentTarget.dataset.id,
    })
  },

})